package app.server.project.main.service.db.entity.user;

import app.server.project.core.syndb.SynDBMgr;
import app.server.project.main.service.service.task.TaskStatusType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_task")
@TableName("user_task")
@Comment(value = "用户任务模块")
public class UserTaskEntity {


    @Id
    @Column(name = "id")
    @Comment("主键")
    @TableId("id")
    private long id;


    @Column(name = "user_id")
    @Comment("用户id")
    @TableField("user_id")
    private long userId;


    @TableField("flush_time")
    @Comment("刷新时间")
    @Column(name = "flush_time")
    private LocalDateTime flushTime;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    @TableField("task_id")
    @Comment("任务类型")
    @Column(name = "task_id")
    private int taskId;

    @TableField("finish_num")
    @Comment("任务完成进度")
    @Column(name = "finish_num")
    private long finishNum;


    @TableField("status")
    @Comment("状态 0 未完成 1 校验态  2 已完成 3 已领取")
    @Column(name = "status")
    private int status;

    public static UserTaskEntity of(long id, int taskId, long userId) {
        var ret = new UserTaskEntity();
        ret.setId(id);
        ret.setSynDb(true);
        ret.setTaskId(taskId);
        ret.setFlushTime(LocalDateTime.now());
        ret.setUserId(userId);
        ret.setStatus(TaskStatusType.UnFinish);
        return ret;
    }

    public void setFlushTime(LocalDateTime flushTime) {
        this.flushTime = flushTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserTaskEntity::getFlushTime, this);
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserTaskEntity::getTaskId, this);
    }

    public void setFinishNum(long finishNum) {
        this.finishNum = finishNum;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserTaskEntity::getFinishNum, this);
    }

    public void addFinishNum(long num) {
        this.finishNum += num;
        this.setFinishNum(this.finishNum);
    }

    public void setStatus(int status) {
        this.status = status;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserTaskEntity::getStatus, this);
    }

    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserTaskEntity::getUserId, this);
    }


}
